package com.grq.mis.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.grq.mis.entity.Order;
import com.grq.mis.exception.ResultEnum;
import com.grq.mis.model.sys.Me;
import com.grq.mis.service.OrderService;
import org.beetl.sql.core.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController{

    @Autowired
    private OrderService orderService;

    /**
     * 商品列表
     *
     * @param model
     * @return
     */
    @SaCheckPermission("order:list")
    @GetMapping("/page")
    public String page(
            Model model,
            @RequestParam(defaultValue = "1") long page,
            @RequestParam(defaultValue = "20") int size,
            String key
    ) {

        Me me = me();

        // 带入参数
        Map<String, Object> paras = new HashMap<>();
        if (!StringUtils.isEmpty(key)) {
            paras.put("key", "%" + key.trim() + "%");
        }
        paras.put("isAdmin",me.getAdmin());
        paras.put("departmentId",me.getDepartmentId());

        PageResult<Order> result = orderService.page("order.page", paras, page, size);
        model.addAttribute("data", result);

        model.addAttribute("key", key);

        return "order/page.html";
    }

    @SaCheckPermission("order:delete")
    @GetMapping("/delete")
    public String delete(@RequestParam Long id, RedirectAttributes attr) {

        Me me = me();
        orderService.deleteOrder(id,me);

        return pageSuccess(attr, "/order/page", ResultEnum.SUCCESS, "删除订单成功");
    }

}
